/**
* 衣服编辑器，用于配置衣服配置的中心，如果有需要就用，这个是随便写写的，也可以自己另外写
*/
namespace ghost{
	export class ClothesEdit extends EditBase{
		public outputTxt:GYText;
		public posTxt:GYText;
		public nameTxt:GYText;
		public frame:GYSprite;
		public nextBtn:GYSprite;
		public rectShape:GYSprite;
		public img:GYSprite;
		public btn:GYSprite;
		private code:string;
		private imgs:string[];
		private _index:number;
		private _startX:number;
		private _startY:number;
		private _endX:number;
		private _endY:number;
		private _rectDrag:boolean;
		constructor(){
			super();
			let s= this;
			s.x = 50;
			s.y = 300;
			s.code = "";
			s._index = -1;
			s.imgs = [
				Conf.role + "common/attack_0.png",
				Conf.role + "red/attack_0.png",
				Conf.role + "purple/attack_0.png",				
				Conf.buff + "300001/300001_stand_up_dd_0000.png",
				Conf.buff + "300002/300002_stand_up_gj_0000.png",
				Conf.buff + "300003/300003_stand_up_gj_0000.png",
				Conf.buff + "300004/300004_stand_up_gjsd_0000.png",
				Conf.buff + "300005/300005_stand_up_gjsd2_0000.png",
				Conf.buff + "300006/300006_stand_up_cw_0000.png",
				Conf.buff + "300007/300007_stand_up_rm_0000.png",
				Conf.buff + "300008/300008_stand_up_xx_0000.png",				
				Conf.monster + "monster101/monster101_attack_up_monster_1_000.png",
				Conf.monster + "monster102/monster102_attack_up_monster_2_000.png",
				Conf.monster + "monster103/monster103_attack_up_monster_3_000.png",
				Conf.monster + "monster104/monster104_attack_up_monster_4_000.png",
				Conf.monster + "monster105/monster105_attack_up_monster_5_000.png",
				Conf.monster + "monster106/monster106_attack_up_monster_6_000.png",
				Conf.monster + "monster107/monster107_attack_up_monster_7_000.png",
				Conf.monster + "monster108/monster108_attack_up_monster_8_000.png",
				Conf.monster + "monster109/monster109_attack_up_monster_9_000.png",
				Conf.monster + "monster110/monster110_attack_up_monster_10_000.png",
				Conf.monster + "boss1/boss1_attack_up_boss_1_00.png",
				Conf.monster + "boss2/boss2_attack_up_boss_2_00.png",
				Conf.monster + "boss3/boss3_attack_up_boss_3_00.png",
				Conf.monster + "boss4/boss4_attack_up_boss_4_00.png",
				Conf.monster + "boss5/boss5_attack_up_boss_5_00.png",
				Conf.bullet + "bullet0.png",
				Conf.bullet + "bullet1.png",
				Conf.bullet + "bullet2.png",
				Conf.bullet + "bullet3.png",
				Conf.bullet + "bullet4.png",
				Conf.effect + "up_effect/up_effect_stand_up_upLv_0000.png",
				Conf.pet + "pet1/pet1_attack_up_pet_000.png"
				];
			s.setSize(600,800);
			s.frame = new GYSprite;
			s.addChild(s.frame);
			s.img = SkinManager.createImage(s,20,210,null);
			


s.rectShape = new GYSprite;
s.rectShape.mouseThrough = true;

			s.outputTxt= SkinManager.createText(s,20,80,"", 0x009900,30,"left",400,40);
			s.posTxt = SkinManager.createText(s,20,130,"", 0,30,"left",400,40);
			s.nameTxt = SkinManager.createText(s,20,170,"", 0,30,"left",400,40);

			s.btn = new GYSprite;
			s.btn.y = s.outputTxt.y;			
			s.btn.x = s.outputTxt.x + 500;
			s.btn.width = 30;
			s.btn.height = 30;
			s.addChild(s.btn);
			s.btn.graphics.drawRect(0,0,30,30,"#ff00ff","#0000ff", 2);

			s.nextBtn = new GYSprite;
			s.nextBtn.y = s.outputTxt.y;			
			s.nextBtn.x = s.btn.x + 40;
			s.nextBtn.width = 30;
			s.nextBtn.height = 30;
			s.addChild(s.nextBtn);
			s.nextBtn.graphics.drawRect(0,0,30,30,"#ffffff","#0000ff", 2);

			s.addChild(s.rectShape);

			s.next();
			s.img.on(Laya.Event.CLICK,s,s.clkImg);
			s.outputTxt.on(Laya.Event.CLICK,s,s.clkTxt);
			s.btn.on(Laya.Event.CLICK,s,s.clkBtn);
			s.nextBtn.on(Laya.Event.CLICK,s,s.clkNextBtn);
			s.on(Laya.Event.MOUSE_MOVE, s,s.move);
			s.img.on(Laya.Event.MOUSE_DOWN,s,s.downImg);
			s.img.on(Laya.Event.MOUSE_UP,s,s.upImg);
		}	
		private move(e:Laya.Event):void
		{
			let s= this;
			let mX:number,mY:number;
			mX = s.img.mouseX;
			mY = s.img.mouseY;
			s.posTxt.text = "图片尺寸：("+s.img.width + "," + s.img.height + "),鼠标位置："+mX+","+mY;
			if(s._rectDrag)
				s.drawRect(mX,mY);
		}
		private clkNextBtn(e:Laya.Event):void
		{
			let s= this;
			s.next();
		}	
		private next():void
		{
			let s= this;
			let str:string;
			str = s.imgs[(++s._index % s.imgs.length)];
			s.img.texture = Main.instance.getRes(str);
			s.nameTxt.text = str.replace(Conf.root,"");
			s.frame.graphics.clear();
			s.frame.graphics.drawRect(s.img.x,s.img.y,s.img.texture.sourceWidth,s.img.texture.sourceHeight,"#000000","#ff0000",2);
		}
		private drawRect(toX:number,toY:number):void		
		{
			let s = this;
			let g:Laya.Graphics = s.rectShape.graphics;
			g.clear();
			g.drawLines(s.img.x,s.img.y,[s._startX,s._startY,toX,s._startY,toX,toY,s._startX,toY,s._startX,s._startY],"#ffff00",2);
			
		}
		private clkBtn(e:Laya.Event):void
		{
			let s= this;
			console.log(s.code);
		}
		private downImg(e:Laya.Event):void
		{
			let s = this;
			if(GYKeyboard.getInstance().isShiftDown())
			{
				s._rectDrag = true;
				s._startX = s.img.mouseX;
				s._startY = s.img.mouseY;
			}
		}
		private upImg(e:Laya.Event):void
		{
			let s = this;
			if(!s._rectDrag)return;
			s._endX = s.img.mouseX;
			s._endY = s.img.mouseY;
			Laya.timer.callLater(s,function(){
				s._rectDrag = false;
				s.rectShape.graphics.clear();
				s.code = s._startX + "," + s._startY + "," + s._endX + "," + s._startY + "," + s._endX + "," + s._endY + "," + s._startX + "," + s._endY;
				s.outputTxt.text = s.code;
			});
		}
		private clkImg(e:Laya.Event):void
		{
			let s = this;
			if(s._rectDrag)return;
			s.code = s.img.mouseX + "," + s.img.mouseY;
			s.outputTxt.text = s.code;
		}
		private clkTxt(e:Laya.Event):void
		{
			let s= this;
			s.code = "";
			s.outputTxt.text = "";			
		}
	}
}